package gang.org.springframework.framework.support;

import gang.org.springframework.framework.bean.GangRootBeanDefinition;
import gang.org.springframework.framework.beanpostprocessor.GangMergedBeanDefinitionPostProcessor;
import gang.org.springframework.framework.context.GangAbstractApplicationContext;
import gang.org.springframework.framework.context.GangApplicationListener;
import gang.org.springframework.framework.factory.config.GangDestructionAwareBeanPostProcessor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 类说明：gang.chen springboot深度实践
 *
 * @author gang.chen
 * @since 2022/5/8
 */
public class GangApplicationListenerDetector implements GangDestructionAwareBeanPostProcessor, GangMergedBeanDefinitionPostProcessor {

    public final transient GangAbstractApplicationContext applicationContext;

    public final transient Map<String, Boolean> singletonNames = new ConcurrentHashMap<>(256);

    public GangApplicationListenerDetector(GangAbstractApplicationContext applicationContext){
        this.applicationContext = applicationContext;
    }

    //TODO
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        return bean;
    }

    /**
     * 业务意义
     * 缓存所有有监听器
     * @param beanDefinition Bean Definition
     * @param beanType Bean Defintion class
     * @param beanName Bean name
     * */
    @Override
    public void postProcessMergedBeanDefinition(GangRootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
        if (GangApplicationListener.class.isAssignableFrom(beanType)) {
            this.singletonNames.put(beanName, beanDefinition.isSingleton());
        }
    }
}
